package com.woniu.controller;

import com.woniu.pojo.PageBean;
import com.woniu.pojo.School;
import com.woniu.pojo.User;
import com.woniu.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
  private   UserService userService;

    /**
     * 改变用户的锁定状态
     * @param
     * @return
     */
    @RequestMapping("changesoftdelDo")
    @ResponseBody
    public Map<String,Object> changesoftdelDo(Integer uid){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            int i =userService .changesoftdel(uid);
            if(i >0 ) {
                studentMap.put("result", true);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 批量锁定/解锁用户
     */
    @RequestMapping("changesoftdelmanyDo")
    @ResponseBody
    public Map<String,Object> changesoftdelDo(Integer[] uid){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            int i = userService.changesoftdelmany(uid);
            if(i >0 ) {
                studentMap.put("result", true);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 新增一个用户
     */
    @RequestMapping("saveoneDo")
    @ResponseBody
    public Map<String,Object> saveoneDo(User user,String rid ){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            int i = userService.saveone(user,rid);
            if(i >0 ) {
                studentMap.put("result", true);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 查看一个用户信息 （修改前展示）
     * @param uid
     * @return
     */
    @RequestMapping("updateDo")
    @ResponseBody
    public Map<String,Object> updateDo(Integer uid){
            Map<String,Object> studentMap = new HashMap<>();
        try{
            User user = userService.selectByuid(uid);
            if(user != null ) {
                studentMap.put("result", true);
                studentMap.put("user", user);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 查看一个用户信息 （修改前展示）
     * @param
     * @return
     */
    @RequestMapping("ustateDo")
    @ResponseBody
    public Map<String,Object> ustateDo(){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            List<String> stateList = userService.selectustate();
            if(stateList.size()>0 ) {
                studentMap.put("result", true);
                studentMap.put("stateList", stateList);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 修改用户 （持久化）
     */
    @RequestMapping("updateoneDo")
    @ResponseBody
    public Map<String,Object> updateoneDo(User user){
        Map<String,Object> studentMap = new HashMap<>();
        try{
            int i = userService.updateone(user);
            if(i >0 ) {
                studentMap.put("result", true);
            }else{
                studentMap.put("result",false);
            }
        }
        catch (Exception e){
            studentMap.put("result",false);
        }
        return studentMap;
    }

    /**
     * 查看用户的角色(不是管理员不能看到管理员这个功能)
     */
    @RequestMapping("roleshowDo")
    @ResponseBody
    public Map<String,Object> permissionshowDo(Integer uid){
        Map<String,Object> permissiontMap = new HashMap<>();
        try{
            Map<String,Object> map = userService.roleshow(uid);
            if(map.size() > 0 ) {
                permissiontMap.put("result", true);
                permissiontMap.put("unassignperissionList",map.get("unassignperissionList"));
                permissiontMap.put("assignpermissionList",map.get("assignpermissionList"));
                permissiontMap.put("uid",uid);
            }else{
                permissiontMap.put("result",false);
            }
        }
        catch (Exception e){
            permissiontMap.put("result",false);
        }
        return permissiontMap;
    }

    /**
     * 增加角色
     */
    @RequestMapping("saveroleDo")
    @ResponseBody
    public Map<String,Object> saveroleDo(Integer uid,Integer rid){
        Map<String,Object> map = new HashMap<>();
        try{
            int i = userService.saverole(uid,rid);
            if(i >0 ) {
                map.put("result", true);
                map.put("uid",uid);
            }else{
                map.put("result",false);
            }
        }
        catch (Exception e){
            map.put("result",false);
        }
        return map;
    }
    /**
     * 收回角色
     */
    @RequestMapping("deleteroleDo")
    @ResponseBody
    public Map<String,Object> deleteroleDo(Integer uid,Integer rid){
        Map<String,Object> map = new HashMap<>();
        try{
            int i = userService.deleterole(uid,rid);
            if(i >0 ) {
                map.put("result", true);
                map.put("uid",uid);
            }else{
                map.put("result",false);
            }
        }
        catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
     * 角色增加权限
     */
    @RequestMapping("savepermissionDo")
    @ResponseBody
    public Map<String,Object> savepermissionDo(Integer pid,Integer rid){
        Map<String,Object> map = new HashMap<>();
        try{
            int i = userService.savepermission(rid,pid);
            if(i >0 ) {
                map.put("result", true);
                map.put("rid",rid);
            }else{
                map.put("result",false);
            }
        }
        catch (Exception e){
            map.put("result",false);
        }
        return map;
    }

    /**
     * 角色减少权限
     */
    @RequestMapping("delectpermissionDo")
    @ResponseBody
    public Map<String,Object> delectpermissionDo(Integer pid,Integer rid){
        Map<String,Object> map = new HashMap<>();
        try{
            int i = userService.deletepermission(rid,pid);
            if(i >0 ) {
                map.put("result", true);
                map.put("rid",rid);
            }else{
                map.put("result",false);
                map.put("rid",rid);
            }
        }
        catch (Exception e){
            map.put("result",false);
        }
        return map;
    }







    //用户列表
    @RequestMapping("userList")
    public String userList(Model model){
        List<User> userList= userService.findAllUser();
        model.addAttribute("userList",userList);
        return "mclazzList02";
    }
    @RequestMapping("changePwd")
    public String changePwd(Integer uid,Model model){
        model.addAttribute("uid", uid);
        return "changePwd";
    }
    //查找某一个人
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String, Object> findOne(HttpSession session){
        Map<String, Object> resultMap=new HashMap<>();
        try {
            User user=(User)session.getAttribute("loginUser");
            User u=userService.findOne(user.getUid());
            //查询授课状态
            userService.findUstate(u);
            //查询所有校区
            List<School> schoolList=userService.findAllArea();
            resultMap.put("schoolList",schoolList);
            resultMap.put("user",u);
            resultMap.put("result",true);
        } catch (Exception e) {
            resultMap.put("result",false);
        }
        return resultMap;
    }


    //跳转个人中心
    @RequestMapping("personalMassage")
    public String personalMassage(){
        return "user/persionMassage";
    }

    //修改个人密码
    @RequestMapping("changePwdDo")
    @ResponseBody
    public Map<String, Object> changePwdDo(String pwd,String pwd1,String pwd2){
        Map<String, Object>resultMap = new HashMap<>();
        try {
            Subject subject = SecurityUtils.getSubject();
            User loginUser = (User) subject.getSession().getAttribute("loginUser");
            if (loginUser.getUpwd().equals(pwd)) {
                Boolean f = userService.changePwd(loginUser, pwd1, pwd2);
                if (f) {
                    resultMap.put("resuleMassage","修改成功");
                    resultMap.put("result", true);
                } else {
                    resultMap.put("resuleMassage","两次密码不一致");
                    resultMap.put("result", false);
                }
            }else{
                resultMap.put("resuleMassage","原密码错误，请检查");
                resultMap.put("result", false);
            }
        } catch (Exception e) {
            resultMap.put("result", false);
            e.printStackTrace();
        }
        return resultMap;
    }

    //修改个人信息
    @RequestMapping("changePersonalMassage")
    @ResponseBody
    public Map<String, Object> changePersonalMassage(User user,HttpSession session){
        Map<String, Object>resultMap = new HashMap<>();
        try {
            userService.updatePersonalMassage(user,session);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
            e.printStackTrace();
        }
        return resultMap;
    }
//    @RequestMapping("getUserName")
//    @ResponseBody
//    public String getUserName(HttpSession session){
//        User user=(User)session.getAttribute("loginUser");
//        return user.getUname();
//    }
}
